package com.szsfm.vehiclemanagement.tcp;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import com.szsfm.vehiclemanagement.bean.User;

public class TcpService extends Service {
    private TcpTask task;
    private Handler handler;
    private TcpCore sav;
    private String TAG = "TcpService";
    public static final String NETWORK_CHANGE = "ConnectivityManager.CONNECTIVITY_ACTION_sfm";

    BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "收到广播");
            if (null != intent && NETWORK_CHANGE.equals(intent.getAction())) {
                Log.i(TAG, "null != intent&&SendBroadcasts.ACTION_NET_CHG");
                TcpUtil.onReceive(context, intent);
            }

        }

    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");
        IntentFilter filter = new IntentFilter();
        filter.addAction(NETWORK_CHANGE);
        registerReceiver(receiver, filter);
        createTask();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createTask();
        if (null != task && task.getStatus() != AsyncTask.Status.RUNNING) {
            task.execute();// 如果异步任务不为空并且没有运行的画，启动
        }
        // 如果网络状况变化的话
        if (null != sav && null != intent && intent.hasExtra("noNetwork") && sav.isruning()) {
            if ("n".equals((intent.getStringExtra("noNetwork")))) {
                sav.onDisconnected();
                try {
                    Log.i("stopNetwork", "stopnetwork");
                    sav.stop();
                    sav = null;
                } catch (InterruptedException e) {
                }
                if (null != task) {
                    task.onPostExecute(null);
                    task.cancel(true);
                    task = null;
                }
            } else if ("y".equals((intent.getStringExtra("noNetwork")))) {
                if (!TcpCore.isConnected.get()) {
                    sav.onConnected();
                }
            }
        }
        return START_FLAG_REDELIVERY;
    }

    private void createTask() {
        User user = User.shareInstance();
        String username = user.getUsername();
        String password = user.getPassword();
        if (task == null || task.isCancelled()) {
            sav = new TcpCore(this, username, password);
            task = new TcpTask(this, sav);
        }
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy");
        if (null != task && task.getStatus() == AsyncTask.Status.RUNNING) {
            task.onPostExecute(null);
            task.cancel(true);
            task = null;
        }
        if (sav != null) {
            try {
                sav.stop();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sav = null;
        }
        unregisterReceiver(receiver);
        super.onDestroy();
    }


}
